import { Controller } from 'egg';

export default class UserController extends Controller {
  public async register() {
    const { ctx, service } = this;
    const validate = {
      username: {
        type: 'string',
        min: 6,
        max: 18,
      },
      password: {
        type: 'string',
        min: 6,
        max: 18,
      },
    };
    ctx.validate(validate);
    ctx.body = await service.user.register(ctx.request.body);
  }

  public async gether() {
    const { ctx, service } = this;
    const validate = {
      userId: 'int',
      gender: {
        type: 'enum',
        values: [ '男', '女' ],
      },
      avatar: 'string',
      nickname: {
        type: 'string',
        max: 12,
        min: 1,
      },
      area: 'string',
      birthday: {
        type: 'string',
        min: 8,
        max: 10,
      },
      height: {
        type: 'int',
        min: 20,
        max: 250,
      },
      motto: {
        type: 'string',
        min: 1,
        max: 50,
      },
    };
    ctx.validate(validate);
    const avatar = await service.avatar.generate(ctx.request.body.avatar);
    ctx.body = await service.user.gether({ ...ctx.request.body, avatar });
  }

  public async login() {
    const { ctx, service } = this;
    const validate = {
      username: 'string',
      password: 'string',
    };
    ctx.validate(validate);
    ctx.body = await service.user.login(ctx.request.body);
  }

  public async logout() {
    const { ctx, service } = this;
    ctx.body = await service.user.logout();
  }

  public async detail() {
    const { ctx, service } = this;
    const validate = {
      userId: 'int',
    };
    ctx.validate(validate);
    ctx.body = await service.user.detail(ctx.request.body.userId);
  }

  public async near() {
    const { ctx, service } = this;
    const validate = {
      userId: 'int',
    };
    ctx.validate(validate);
    ctx.body = await service.user.near(ctx.request.body.userId);
  }

  public async online() {
    const { ctx, service } = this;
    const validate = {
      userId: 'int',
    };
    ctx.validate(validate);
    ctx.body = await service.user.online(ctx.request.body.userId);
  }
}
